/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wargame.controller;

import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import module.GameSetting;
import module.GameTurn;
import module.GlobalConstant;
import module.ManageEmployee;
import module.ManagePlayer;
import module.Utility_Function;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import wargame.dao.TransactionDAO;
import wargame.entity.Player;
import wargame.entity.Transaction;
import wargame.entity.TransactionTurn;

/**
 *
 * @author stanley.2007
 */
@Controller
@RequestMapping(value = "/transaction")
public class TransactionController {

    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public ModelAndView viewProjectBids(@RequestParam(value = "announcement", required = false) String announcement,
            HttpServletRequest request, Model model) {
        // model refer to object to pass to this page
        // check index.jsp for the ${username}
        ModelAndView mv = new ModelAndView();

        Player player = ManagePlayer.getThisPlayer(request);

        if (player == null) {
            mv.setViewName("register");
            return mv;
        }

        ArrayList<Transaction> transaction_list = TransactionDAO.retrieveAllPlayerTransaction(player.getPlayer_id());

        ArrayList<TransactionTurn> transaction_turn_list = new ArrayList<TransactionTurn>();
        int count_turn = TransactionDAO.countPlayerTurn(player.getPlayer_id());

        // iniitalize variables
        for (int i = 0; i < count_turn; i++) {
            transaction_turn_list.add(new TransactionTurn());
        }

        int balance = GlobalConstant.PLAYER_STARTING_AMOUNT;
        int remaining_balance = GlobalConstant.PLAYER_STARTING_AMOUNT;
        int prev_turn = -1;
        int change_in_balance = 0;
        TransactionTurn tempContainer = new TransactionTurn();

        for (int j = transaction_list.size() - 1; j > -1; j--) {

            Transaction transaction = transaction_list.get(j);

            if (transaction.getTurn() != prev_turn) {
                remaining_balance += change_in_balance;
                tempContainer.setRemaining_balance(remaining_balance);

                tempContainer = transaction_turn_list.get(count_turn - 1);
                tempContainer.setTurn(transaction.getTurn());
                count_turn--;
                change_in_balance = 0;
                prev_turn = transaction.getTurn();
            }

            if (transaction.getCredit_amount() > 0) {
                balance -= transaction.getCredit_amount();
                change_in_balance -= transaction.getCredit_amount();
            }

            if (transaction.getDebit_amount() > 0) {
                balance += transaction.getDebit_amount();
                change_in_balance += transaction.getDebit_amount();
            }

            transaction.setBalance(balance);
            tempContainer.addTransaction(transaction);
        }
        remaining_balance += change_in_balance;
        tempContainer.setRemaining_balance(remaining_balance);

        int rentalNextTurn = GameSetting.rentalNextTurn(player);
        int employeeSalaryNextTurn = ManageEmployee.payEmployeesSalaryNextTurn(player);
        remaining_balance -= rentalNextTurn + employeeSalaryNextTurn;

        mv.setViewName("transaction");
        mv.addObject("player", player);
        mv.addObject("turn", GameTurn.getTurnInfo());
        mv.addObject("transaction_turn_list", transaction_turn_list);
        mv.addObject("next_employee_salary", Utility_Function.formatCurrency(employeeSalaryNextTurn));
        mv.addObject("next_rental", Utility_Function.formatCurrency(rentalNextTurn));
        mv.addObject("next_balance", Utility_Function.formatCurrency(remaining_balance));
        return mv;
    }
}
